<template>
	<view class="setpaypass">
		<view class="contant_box my-pdlr-15">
			<view class="contant_box_li">
				<text>{{phone}}</text>
			</view>
			<view class="contant_box_li">
				<input type="text" class="inp" v-model="verificationCode" placeholder-class="inp_pla"
					placeholder="请输入短信验证码" />
				<view class="contant_box_li_txt">
					<view v-if="anew">{{countDownNum}}s后重新获取</view>
					<view v-else @click="getCode">获取验证码</view>
				</view>
			</view>
			<view class="contant_box_li">
				<input type="text" class="inp" 
				v-model="newPassword" 
				placeholder-class="inp_pla"
				placeholder="请设置新支付密码" />
				<image :src="close_eye" v-show="seen" class="icon" @click="seen=!seen"/>
				<image :src="open_eye" v-show="!seen" class="icon" @click="seen=!seen"/>
			</view>
			<view class="txt_box">
				<text>密码为6位数字组合</text>
			</view>
			<view class="complete">
				<button class="btn" @click="complete">完成</button>
			</view>
		</view>
		<u-popup v-model="QSPopup" mode="center">
			<view class="codebox">
				<view class="tlt my-mgb-10">请输入图形验证码</view>
				<view class="image-code">
					<input type="text" v-model="verifyCode" class="input" placeholder="输入图片中的图形" />
					<view class="img" @click="getVCode">
						<image :src="imageCode" mode="" class="code-img"></image>
						<view class="tishi">看不清？换一张</view>
					</view>
				</view>
				<view class="btnbox">
					<view class="left" @click="cancel">取消</view>
					<view class="right" @click="sendCode">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {getVerifyCodeApi,sendMsgApi} from "@/api/uesr.js"
	import { setpaypasswordApi} from "@/api/myinter.js"
	import { createNamespacedHelpers } from "vuex";
	const { mapState } = createNamespacedHelpers( "my");
	export default {
		data() {
			return {
				verificationCode: '', //短信验证码
				countDownNum: 60, // 倒计时初始值
				anew: false, //重新获取
				imageCode: '', //图形验证码
				QSPopup: false, //图形验证控制
				verifyCode: '',
				newPassword:'',//新密码
				seen:true,
				open_eye:'https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/OSUdUkrYKu_open_eye.png',
				close_eye:'https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/snVKZMXaHU_close_eye.png',
			};
		},
		computed:{
			...mapState(["phone"]),
		},
		methods: {
			cancel(){
				this.QSPopup = false;
			},
			async complete(){
				let params = {
					amount:'',
					memberBankId:'',
					payPassword:this.newPassword,
					smsCode:this.verificationCode
				}
				uni.showLoading({
					title:"正在加载中...."
				})
				const res = await setpaypasswordApi(params)
				if(res.statusCode ===200){
					uni.hideLoading()
					uni.showToast({
						title:"设置成功",
						icon:"success",
						duration:2000,
						success() {
							uni.navigateTo({
								url:"/pages/my/personalData"
							})
						}
					})
				}
			},
			// 验证码倒计时
			getCode() {
				if (this.phone) {
					var regex = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
					var bool = regex.test(this.phone)
					if (bool) {
						this.QSPopup = true
						this.getVCode()
					} else {
						uni.showToast({
							title: "请输入正确的手机号",
							icon: "none"
						})
					}
				} else {
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
				}
			},
			// 获取图形验证码
			async getVCode() {
				uni.showLoading({
					title: "正在加载中...."
				})
				const res = await getVerifyCodeApi({})
				if (res.statusCode === 200) {
					uni.hideLoading()
					let imageBase64 = 'data:image/png;base64,' + wx.arrayBufferToBase64(res.data)
					this.imageCode = imageBase64
				} else {
					uni.showToast({
						title: '出错了，请重新获取',
						icon: "none"
					})
				}
			},
			// 发送验证码
			async sendCode() {
				if (this.verifyCode === "") {
					uni.showToast({
						title: "请先输入图形验证码！",
						icon: "none"
					})
					return
				} else {
					let params = {
						phone: this.phone,
						verifyCode: this.verifyCode
					}
					uni.showLoading({
						title: '正在加载中....'
					})
					const res = await sendMsgApi(params)
					console.log(res, "res");
					if (res.statusCode == 200) {
						uni.hideLoading()
						this.QSPopup = false;
						this.anew = true
						this.countDown()
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				}
			},
			// 倒计时
			countDown() {
				var _this = this;
				_this.anew = true
				var countDownNum = _this.countDownNum // 获取倒计时初始值
				var timer = setInterval(() => {
					countDownNum -= 1
					_this.countDownNum = countDownNum
					if (countDownNum <= -1) {
						clearInterval(timer)
						// 取消置顶的setInterval函数将要执行的代码
						_this.countDownNum = 60
						_this.anew = false
					}
				}, 1000)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.setpaypass {
		width: 100%;
		height: 100vh;
		background: #FFFFFF;

		.contant_box {
			width: 100%;
			height: 1165rpx;

			.contant_box_li {
				width: 100%;
				height: 100rpx;
				border-bottom: 1rpx solid #F5F5F5;
				display: flex;
				align-items: center;
				font-size: 28rpx;
				font-weight: 500;
				color: #999999;
				position: relative;
				.inp {
					width: 65%;
					line-height: 100rpx;
				}
				.icon{
					width: 34rpx;
					height: 34rpx;
					position: absolute;
					top: 35rpx;
					right: 40rpx;
				}

				.inp_pla {
					font-size: 28rpx;
					font-weight: 500;
					color: #999999;
				}

				.contant_box_li_txt {
					width: 35%;
					height: 100rpx;
					line-height: 100rpx;
					text-align: center;
					font-size: 28rpx;
					font-weight: 500;
					color: #F05329;
				}
			}
		}
		.txt_box{
			width: 100%;
			height:100rpx ;
			line-height: 100rpx;
			font-size: 22rpx;
			font-weight: 400;
			color: #F05329;
		}
		.complete{
			width: 100%;
			height: 200rpx;
			display: flex;
			align-items: flex-end;
			.btn{
				width: 690rpx;
				line-height: 88rpx;
				margin: 0 auto;
				background: #F05329;
				border-radius: 10rpx;
				font-size: 38rpx;
				font-weight: 500;
				color: #FFFFFF;
			}
		}

		.codebox {
			width: 690rpx;
			height: 376rpx;
			margin: auto;
			background: #FFFFFF;
			border-radius: 20rpx;

			.tlt {
				width: 100%;
				height: 110rpx;
				text-align: center;
				line-height: 110rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.image-code {
				width: 100%;
				height: 130rpx;
				display: flex;
				padding-left: 30rpx;

				.input {
					font-size: 32rpx;
					color: #000;
					width: 350rpx;
					height: 70rpx;
					border: 1rpx solid #999999;
					border-radius: 10rpx;
					margin-right: 30rpx;
					padding-left: 20rpx;
				}

				.img {
					width: 270rpx;
					height: 100%;

					.code-img {
						width: 90%;
						height: 80upx;
					}

					.tishi {
						width: 90%;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}

			.btnbox {
				width: 100%;
				height: 95rpx;
				margin-top: 23rpx;
				display: flex;
				border-top: 1rpx solid #999999;

				.right,
				.left {
					width: 50%;
					height: 100%;
					text-align: center;
					line-height: 95rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
				}

				.right {
					color: #F05329;
					border-left: 1rpx solid #999999;
				}

				.left {
					color: #999999;
				}
			}
		}
	}
</style>
